package com.atguigu.mp.test;

import com.atguigu.mp.pojo.User;
import com.atguigu.mp.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.Wrapper;

// 2.演示条件构造器

@SpringBootTest
public class MPTest2 {

    @Autowired
    private UserService userService;

    //1.QueryWrapper
    @Test
    public void test01(){
        // Wrapper: 包装器
        // QueryWrapper： 查询条件包装器
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        // wrapper的多个条件默认是and相连。如果想要表示or，则需要or()方法
        //wrapper.gt("age",20);
        //wrapper.eq("email","zhangsanfeng@atguigu.com");

        wrapper.gt("age",20).or().eq("email","zhangsanfeng@atguigu.com");

        userService.list(wrapper).forEach(System.out::println);
}

    //2.UpdateWrapper
    @Test
    public void test02(){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set("name","hanmeimei");
        wrapper.set("age",99);
        wrapper.set("email","hanmeimei@atguigu.com");
        wrapper.eq("id",99l);
        userService.update(wrapper);
    }

    //3. condition
    @Test
    public void test03(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User userTemp = new User();
        userTemp.setUage(20);
        wrapper.gt(userTemp.getUage()!=null,"age",userTemp.getUage());
        userService.list(wrapper).forEach(System.out::println);
    }
    @Test
    public void test04(){
        User userTemp = new User();
        userTemp.setUage(20);
        userTemp.setUemail("xxx@sina.com.cn");
        userTemp.setUid(99l);

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();

        wrapper.set(userTemp.getUname()!=null,"name",userTemp.getUname());
        wrapper.set(userTemp.getUage()!=null,"age",userTemp.getUage());
        wrapper.set(userTemp.getUemail()!=null,"email",userTemp.getUemail());
        wrapper.eq(userTemp.getUid()!=null,"id",userTemp.getUid());

        userService.update(wrapper);
    }

    //4. LambdaQueryWrapper , LambdaUpdateWrapper
    @Test
    public void test05(){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        User userTemp = new User();
        userTemp.setUage(20);
        // LambdaQueryWrapper的好处是，不需要硬编码列名称
        wrapper.gt(userTemp.getUage()!=null,User::getUage,userTemp.getUage());
        userService.list(wrapper).forEach(System.out::println);
    }
}
